package view
{
	import com.greensock.TweenLite;
	import com.greensock.easing.Back;
	
	import data.Globals;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	
	import flashx.textLayout.formats.TextAlign;
	
	import view.base.BaseView;
	
	public class CaptionView extends BaseView
	{
		private var txt:TextField;
		
		private var timeNum:Number = 0;
		
		private var _call:Function;
		public function CaptionView()
		{
			super();
			this.mouseChildren = false;
			this.mouseEnabled = false;
			this.x = Globals.stage_width/2;
			this.y = Globals.stage_height/2;
			var maskSp:Sprite = new Sprite();
			this.addChild(maskSp);
			maskSp.graphics.beginFill(0xffffff);
			maskSp.graphics.drawRect(0,0,300,100);
			maskSp.graphics.endFill();
			maskSp.x = -maskSp.width/2;
			maskSp.y = -maskSp.height/2;
			
			var tformat:TextFormat = new TextFormat("宋体",16,0x000000,true,null,null,null,null,TextAlign.CENTER);
			
			txt = new TextField();
			this.addChild(txt);
			txt.mask = maskSp;
			txt.width = 300;
			txt.x = -150;
			txt.wordWrap = true;
			txt.autoSize = TextFieldAutoSize.LEFT;
			txt.defaultTextFormat = tformat;
		}
		
		public function show(str:String,callback:Function,tfmat:TextFormat = null):void{
			txt.text = str;
			_call= callback;
			if(tfmat){
				txt.setTextFormat(tfmat);
			}
		
			isCloseLoop = false;
			txt.y = 50;
			txt.alpha = 1;
		}
		
		override protected function loop(event:Event):void{
			if(isCloseLoop)return;
			if(txt.y+txt.height - 100>=-50){
				txt.y--;
			}else{
				timeNum++;
				if(timeNum/30>3){
					isCloseLoop = true;
					TweenLite.to(txt, 1, {alpha:0,ease:Back.easeOut ,onComplete:remove});
				}
			}
		}
		
		private function remove():void{
			if(this.parent){
				this.parent.removeChild(this);
			}
			if(_call != null)_call();
		}
	}
}